﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Sis.Entities;
using Sis.Service;

namespace Sis.Web.Controllers
{
    public class VentaRealizadaController : Controller
    {
        private readonly IPromotorService _promotorService;
        private readonly IVentaRegistradaService _ventaRelizadaService;
        public VentaRealizadaController(IPromotorService promotorService,
            IVentaRegistradaService ventaRelizadaService
            )
        {
            _ventaRelizadaService = ventaRelizadaService;
            _promotorService = promotorService;

        }

        public ActionResult Index()
        {
            var lista = _ventaRelizadaService.GetAllFRomVentasREsitradas("");
            return View(lista);
        }
        public ActionResult Reportes()
        {

            return View("Reportes");
        }
        [HttpPost]
        public ActionResult Index(string criterio)
        {
            var lista = _ventaRelizadaService.GetAllFRomVentasREsitradas(criterio);
            return View(lista);
        }

        //
        // GET: /VentaRealizada/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /VentaRealizada/Create

        public ActionResult Create()
        {
            ViewData["IdPromotor"] = new SelectList(_promotorService.GetPromotores().ToList(), "IdPromotor", "Nombres");
            IEnumerable<SelectListItem> items = _promotorService.GetPromotores().Select(c => new SelectListItem
            {
                Value = c.Id.ToString(),
                Text = c.Nombres
            });
            ViewData["IdPromotor"] = items;
            return View("Create");
        }

        //
        // POST: /VentaRealizada/Create

        [HttpPost]
        public ActionResult Create(VentaRegistrada ventaRealizada)
        {
            try
            {
                // TODO: Add insert logic here
                if (ModelState.IsValid)
                {
                    _ventaRelizadaService.AddVentaResotrada(ventaRealizada);
                    return RedirectToAction("Index");
                }
                else
                {
                    ViewData["IdPromotor"] = new SelectList(_promotorService.GetPromotores().ToList(), "IdPromotor", "Nombres");
                    IEnumerable<SelectListItem> items = _promotorService.GetPromotores().Select(c => new SelectListItem
                    {
                        Value = c.Id.ToString(),
                        Text = c.Nombres
                    });
                    ViewData["IdPromotor"] = items;
                    return View("Create", ventaRealizada);
                }

                
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /VentaRealizada/Edit/5

        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /VentaRealizada/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /VentaRealizada/Delete/5

        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /VentaRealizada/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
